<?php

namespace App\Repository\Commit;

use App\Models\DictData as model;
use App\Repository\BaseRepository;

class DictDatarRepository extends BaseRepository
{
    public function __construct()
    {
        $this->model = new model;
        parent::__construct();
    }

    public function index($request)
    {
        $where['like'] = ['order_nums'];
        $cron['like'] = $this->getWhere($where['like'], $request);
        if (!empty($request->user->company_master_id)) {
            $cron['where'] = $this->getWhere(['company_master_id'], $request);
        }
        if (!empty($request['created_start']) && !empty($request['created_end'])) {
            $cron['between']['create_time'][] = $request['created_start'];
            $cron['between']['create_time'][] = $request['created_end'];
        }

        $cron['order']['orderby'] = isset($request['orderby']) ? $request['orderby'] : 'id';
        $cron['order']['orderdir'] = isset($request['orderdir']) ? $request['orderdir'] : 'desc';
        $cron['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
        $cron['limit']['number'] = $request->get('pageSize');
        $rows = $this->search($this->model, $cron);
//        echo $this->getLastSql();
        return $rows;
    }

    public function store($request)
    {
        // if($this->model->where(['order_nums'=>$request->get('order_nums')])->first()){
        //     return $this->result(-1,'该数据已添加');
        // }
        if (!empty($request->user->id)) {
            $request->offsetSet('users_id', $request->user->id);
        }
        if (!empty($request->user->company_master_id)) {
            $request->offsetSet('company_master_id', $request->user->company_master_id);
        }
        $request->offsetSet('create_time', date('Y-m-d H:i:s'));
        // if(empty($request->get('order_num'))){
        //     $request->offsetSet('order_num', );
        // }
        $res = $this->model->create($request->all());
        return $this->returnMsg(0, '添加成功', $res);
    }

    public function save($request, $id)
    {
        if (!empty($request->get('name'))) {
            $count = $this->model->where(['name' => $request->get('name')])->count();
            if ($count > 1) {
                return $this->returnMsg(-1, '该数据已添加');
            }
        }
        $userRes = $this->model->where('order_num', $id)->first();
        if (!$userRes) {
            //修改订单的状态
            $this->orderModel->where('order_num', $id)->update(['state' => 3]);

            return $this->store($request);
        }
        foreach ($this->model->getFillable() as $v) {
            if (isset($request[$v]) && $request[$v] != '') {
                $userRes->$v = $request[$v];
            }
        }
        $userRes->update_time = date('Y-m-d H:i:s');
        $userRes->save();
        $this->orderModel->where('order_num', $id)->update(['state' => 3]);
        return $this->returnMsg(0, '修改成功', true);
    }

    public function show($id)
    {
        $res = $this->model->where('order_num', $id)->first();
        return $this->returnMsg(0, '获取成功', $res);
    }

    public function destroy($id)
    {
        $this->model->destroy($id);
        return $this->returnMsg(0, '删除成功', true);
    }

    public function oneSel($type)
    {
        $where = [
            'dict_type' => $type,
            'status' => 0,
        ];
        $resData = $this->model->where($where)->select([
            'dict_code',
            'dict_name',
            'dict_label',
            'dict_value',
            'dict_type',
            'status',
            'is_default'])->get();
        $returnData = [];
        if (!empty($resData)) {
            $returnData = usfirstArr($resData);
        }
        return $this->returnMsg(200, '查询成功', $returnData);
    }

    public function setTypeValue($type, $value)
    {
        $where = [
            'dict_type' => $type
        ];
        $this->model->where($where)->update(['dict_value' => $value]);
    }
//获取字段类型数据，
// @param $type 可以数组与字符
    public function getTypeValue($type)
    {
        $where = [];
        $where['eq']['status'] = 0;
        if (is_array($type)) {
            $where['in']['dict_type'] = $type;
        } else {
            $arr = explode(',', $type);
            if (count($arr) > 1) {
                $where['in']['dict_type'] = $arr;
            } else {
                $where['eq']['dict_type'] = $type;
            }
        }
        $resData = $this->model->searchWhere($where)->select(['dict_type', 'dict_value'])->get();
//        echo $this->getLastSql();
        $returnData = [];
        if (!empty($resData)) {
            foreach ($resData as $row) {
                $returnData[$row->dict_type] = $row->dict_value;
            }
        }
        return $returnData;
    }

//    多项选择的类型选项
    public function getTypeOptions($type)
    {
        $where = [];
        $where['eq']['status'] = 0;
        if (is_array($type)) {
            $where['in']['dict_type'] = $type;
        } else {
            $arr = explode(',', $type);
            if (count($arr) > 1) {
                $where['in']['dict_type'] = $arr;
            } else {
                $where['eq']['dict_type'] = $type;
            }
        }
        $resData = $this->model->searchWhere($where)->select(['dict_type', 'dict_value', 'dict_label'])->get();
        $returnData = [];
        if (!empty($resData)) {
            foreach ($resData as $row) {
                $returnData[$row->dict_type][] = ['value' => $row->dict_value, 'label' => $row->dict_label];
            }
        }
        return $returnData;
    }

    public function getOne($type)
    {
        $where = [
            'dict_type' => $type,
            'status' => 0,
        ];
        $resData = $this->model->where($where)->select([
            'dict_code',
            'dict_name',
            'dict_label',
            'dict_value',
            'is_default'])->first();
        return $resData;
    }

//取单个值
    public function singleTypeValue($type)
    {
        $data = $this->getOne($type);
        return $data['dict_value'] ?? '';
    }

    public function typeOptions()
    {
        $resData = $this->model->select(['dict_name', 'dict_type'])->distinct()->get();
        return $resData;
    }

}
